package com.usian.search.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.usian.model.behavior.pojos.ApBehaviorEntry;
import com.usian.model.common.base.BaseServiceImpl;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.search.dtos.UserSearchDto;
import com.usian.model.search.pojos.ApUserSearch;
import com.usian.model.user.pojos.ApUser;
import com.usian.search.feign.BehaviorFeign;
import com.usian.search.mapper.ApUserSearchMapper;
import com.usian.search.service.ApUserSearchService;
import com.usian.utils.threadlocal.AppThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author zlm
 * @version 1.0
 * @date 2022/9/28 20:00
 */
@Service
public class ApUserSearchServiceImpl extends BaseServiceImpl<ApUserSearchMapper, ApUserSearch> implements ApUserSearchService {
    @Autowired
    private BehaviorFeign behaviorFeign;

    @Override
    public ResponseResult findUserSearch(UserSearchDto userSearchDto) {
        if (userSearchDto.getPageSize() > 50) return paramError;
        ApUser user = AppThreadLocalUtils.getUser();
        ApBehaviorEntry apBehaviorEntry = behaviorFeign.findByUserIdOrEquipmentId(user.getId() != null ? user.getId():-1, userSearchDto.getEquipmentId() != null ? userSearchDto.getEquipmentId():-1);
        if (apBehaviorEntry == null) return paramDataExist;
        Page<ApUserSearch> page = new Page<>(userSearchDto.getPageNum(), userSearchDto.getPageSize());
        IPage<ApUserSearch> iPage = page(page, Wrappers.<ApUserSearch>lambdaQuery().eq(ApUserSearch::getEntryId, apBehaviorEntry.getId()).eq(ApUserSearch::getStatus, 1));
        return ResponseResult.okResult(iPage.getRecords());
    }

    @Override
    public ResponseResult delUserSearch(UserSearchDto userSearchDto) {
        if (userSearchDto.getId() == null) return paramError;
        update(Wrappers.<ApUserSearch>lambdaUpdate().eq(ApUserSearch::getId,userSearchDto.getId()).set(ApUserSearch::getStatus,0));
        return okResult;
    }

    @Async("searchHistory")
    @Override
    //搜索历史 查询(根据实体Id和查询关键字查询)
    public void insert(Integer entryId, String searchWords) {
        ApUserSearch one = getOne(Wrappers.<ApUserSearch>lambdaQuery().eq(ApUserSearch::getEntryId, entryId).eq(ApUserSearch::getKeyword, searchWords));
        if (one == null) {
            ApUserSearch apUserSearch = new ApUserSearch();
            apUserSearch.setEntryId(entryId);
            apUserSearch.setCreatedTime(new Date());
            apUserSearch.setKeyword(searchWords);
            apUserSearch.setStatus(1);
            save(apUserSearch);
        } else {
            if (one.getStatus() == 0) {
                one.setStatus(1);
                updateById(one);
            }
        }
    }
}
